package IC.SymbolTable;

import java.util.HashMap;
import java.util.LinkedHashMap;

import IC.Constants;
import IC.SemanticChecks.SemanticError;
import IC.SemanticChecks.SemanticErrorType;

public class StaticMethodSymbolTable extends LookupSymbolTable {

	private final LinkedHashMap<String,Symbol> Formals;
	private final LinkedHashMap<String,Symbol> Locals;
	
	//C'tor
	public StaticMethodSymbolTable(String id) {
		super(id,id);
		Formals = new LinkedHashMap<String,Symbol>();
		Locals = new LinkedHashMap<String,Symbol>();
	}
	
	@Override
	public Symbol getValue(String id) {
		if (getFormals().containsKey(id)) {
			return getFormals().get(id);
		} else {
			return Locals.get(id);
		}
	}
	
	@Override
	protected HashMap<String,Symbol> getInnerHashMap(SymbolKind kind, int lineNumber) throws SemanticError {
		switch (kind) {
		case FORMAL:
			return getFormals();
		case LOCAL:
		case RETURN_VARIABLE:
			return Locals;
		default:
			throw new SemanticError(SemanticErrorType.INVALID_KIND, lineNumber);
		}
	}
	
	@Override
	public Symbol staticLookup(String id) {
		return getParent().staticLookup(id);
	}
	
	public LinkedHashMap<String,Symbol> getFormals() {
		return Formals;
	}

	@Override
	public String toString() {
		String str = Title() + Constants.LINE_BREAK;
		for (Symbol s : getFormals().values()) 
			str += Constants.SPACE + s + Constants.LINE_BREAK;	
		
		for (Symbol s : Locals.values()) 
			if (s.getKind() != SymbolKind.RETURN_VARIABLE) 
				str += Constants.SPACE + s + Constants.LINE_BREAK;	
		
		str += childrensToString();
		return str;
	}

	@Override
	public String Title() {
		return "Method Symbol Table: " + getDisplayId();
	}

}
